﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using MarketOnlineDTO;
using MySql.Data.MySqlClient;

namespace MarketOnlineDAO
{
    public class ThamSoDAO : BaseDataProvider
    {
        MySqlCommand _cmd;

        public Boolean ThemThamSo(ThamSoDTO ts)
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "insert into ThamSo(TenThamSo,GiaTriThamSo) values(@TenThamSo,@GiaTriThamSo)";
            _cmd.CommandType = CommandType.Text;
            _cmd.Parameters.Add(new MySqlParameter("@TenThamSo", ts.TenThamSo));
            _cmd.Parameters.Add(new MySqlParameter("@GiaTriThamSo", ts.GiaTriThamSo));            

            return ExecuteNonQuery(_cmd);
        }

        public Boolean XoaThamSo(ThamSoDTO ts)
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "delete from ThamSo where MaThamSo = @MaThamSo";
            _cmd.CommandType = CommandType.Text;
            _cmd.Parameters.Add(new MySqlParameter("@MaThamSo", ts.MaThamSo));            

            return ExecuteNonQuery(_cmd);
        }

        public Boolean CapNhatThamSo(ThamSoDTO ts)
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "update ThamSo set GiaTriThamSo = @GiaTriThamSo where MaThamSo = @MaThamSo";
            _cmd.CommandType = CommandType.Text;
            _cmd.Parameters.Add(new MySqlParameter("@MaThamSo", ts.MaThamSo));
            _cmd.Parameters.Add(new MySqlParameter("@GiaTriThamSo", ts.GiaTriThamSo));

            return ExecuteNonQuery(_cmd);
        }

        public List<ThamSoDTO> LayDanhSachThamSo()
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "select * from ThamSo";
            _cmd.CommandType = CommandType.Text;

            DataTable dt = new DataTable();

            dt = ExecuteSelectCommand(_cmd);

            if (dt.Rows.Count <= 0)
            {
                return null;
            }

            List<ThamSoDTO> list = new List<ThamSoDTO>();

            foreach (DataRow row in dt.Rows)
            {
                ThamSoDTO dto = new ThamSoDTO();
                dto.MaThamSo = int.Parse(row["MaThamSo"].ToString());
                dto.TenThamSo = row["TenThamSo"].ToString();
                dto.GiaTriThamSo = row["GiaTriThamSo"].ToString();

                list.Add(dto);
            }

            return list;
        }

    }
}
